
#include <windows.h>

#include "lib.h"
#include "patch.h"
#include "admlist.h"
#include "admpipe.h"

#include "patchsrv.h"
#include "pkglist.h"
#include "pkgdesc.h"

void
AdminListPkg::reset()
{
	index = 0;
}

void
AdminListPkg::handle(uint8* msg, int32 len)
{
	if (pipe->isLoggedOn())
	{
		PackageList* list;
		PackageDescription* pkg;

		list = PatchServer::This->getPackageList();
		if (!list)
		{
			pipe->sendError(APE_MEMORY_ERROR);
			return;
		}

		if (*msg == 'L')
		{
			index = 0;

			pipe->msgStart();
			pipe->msgAdd((uint8)'S');
			pipe->msgAdd((uint8)0x01);
			pipe->msgAdd((uint8)(list->getCount() & 0xff));
			pipe->msgEnd();

			if (list->getCount() == 0)
			{
				PatchServer::This->releasePackageList();
				return;
			}
		}

		pkg = list->getItem(index);
		if (!pkg)
		{
			PatchServer::This->releasePackageList();
			pipe->sendError(APE_LIST_NO_MORE);
			return;
		}

		while (pkg)
		{
			if (!pkg->adminSend(pipe))
				break;

			++index;
			pkg = pkg->getNext();
		}

		PatchServer::This->releasePackageList();
	}
	else
		pipe->sendError(APE_UNKNOWN_REQUEST);
}
